var express = require('express');
var router = express.Router();

/* GET home page.
* /路径就渲染index页面  并传参{ title: 'Express' } */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
// 注册一个路由: 用户注册
/*
a)path为: /register
b)请求方式为: POST
c)接收username和password参数
d)admin是已注册用户
e)注册成功返回: {code: 0, data: {_id: 'abc', username: ‘xxx’, password:’123’}
f)注册失败返回: {code: 1, msg: '此用户已存在'}
 */
/*
1. 获取请求参数
2. 处理
3. 返回响应数据
 */
router.post('/register', function(req, res, next) {
    const {username, password} = req.body
    console.log('register', username, password)
    if (username === 'admin') {
        res.send({code: 1, msg: '此用户已存在'})
    } else {
        res.send({code: 0, data: {_id: 'abc', username, password}})
    }

});

module.exports = router;
